package chitietvanban.backing;

import chitietvanban.chitietvanban;

import chitietvanban.resource.chitietvanbanBundle;

import com.bea.core.repackaged.springframework.core.io.UrlResource;

import common.Utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.WindowState;

import javax.servlet.ServletContext;

import oracle.adf.model.BindingContext;

import oracle.adf.model.binding.DCIteratorBinding;

import oracle.binding.AttributeBinding;
import oracle.binding.BindingContainer;

import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaItem;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.ViewObject;
import oracle.jbo.common.JboCompOper;
import oracle.jbo.domain.BlobDomain;

import org.apache.commons.io.IOUtils;

public class chitietvanbanBacking {
    private String mPortletTitle;
    private String vanbanId;
    private String chitietvanbanUrl;
    private String tieudechitietvb;
    private String UrlFiledowload;
    private String langid;

    public String getViewRender() {
        getLangid();
        getVanbanId();
        getVanbanKhac();
        return null;
    }

    public String getPortletTitle() {
        if (mPortletTitle == null) {
            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            PortletPreferences preferences = request.getPreferences();
            mPortletTitle =
                    preferences.getValue(chitietvanban.PORTLETTITLE_KEY,
                                         getResource(chitietvanbanBundle.PORTLETTITLE));
        }
        return mPortletTitle;
    }

    public String getPortletTitleLabel() {
        String resource = getResource(chitietvanbanBundle.PORTLETTITLE);
        return resource;
    }

    public void setPortletTitle(String portletTitle) {
        mPortletTitle = portletTitle;
    }

    public String applyAction() throws Exception {
        PortletRequest request =
            (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        PortletPreferences preferences = request.getPreferences();
        preferences.setValues(chitietvanban.PORTLETTITLE_KEY,
                              mPortletTitle.split(","));
        preferences.store();
        return null;
    }

    public String getApplyLabel() {
        String resource = getResource(chitietvanbanBundle.APPLY_LABEL);
        return resource;
    }

    public String okAction() throws Exception {
        applyAction();
        ActionResponse response =
            (ActionResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
        response.setPortletMode(PortletMode.VIEW);
        response.setWindowState(WindowState.NORMAL);
        return null;
    }

    public String getOkLabel() {
        String resource = getResource(chitietvanbanBundle.OK_LABEL);
        return resource;
    }

    private String getResource(String resourceKey) {
        PortletRequest request =
            (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        PortletConfig config =
            (PortletConfig)request.getAttribute("javax.portlet.config");
        return config.getResourceBundle(request.getLocale()).getString(resourceKey);
    }


    private void getVanbanKhac() {
        DCIteratorBinding vbkhacIterator =
            Utils.getIteratorBinding("DtvbNoidungvanbanKhac1Iterator");
        ViewObject vo = vbkhacIterator.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        vc.setName("VBKhacViewCriteria");
        ViewCriteriaRow vcRow = vc.createViewCriteriaRow();
        ViewCriteriaItem rowViewCriteriaItem = vcRow.ensureCriteriaItem("Id");
        rowViewCriteriaItem.setOperator(JboCompOper.OPER_NE);
        rowViewCriteriaItem.setValue(vanbanId);
        vc.addRow(vcRow);
        vo.getViewCriteriaManager().removeApplyViewCriteriaName("VBKhacViewCriteria");
        vo.applyViewCriteria(vc);
        vo.executeQuery();
    }

    private String UrlChitietvanban() {
        String urlChitiet = "";
        try {
            urlChitiet =
                    Utils.getPortletRequest().getParameter("Urlchitietvb");
        } catch (Exception e) {
            // TODO: Add catch code
            e.printStackTrace();
        }

        return urlChitiet;
    }
    
    private String GetUrlDowloadfile() {
        String dowlaodfile = "";
        try {
            dowlaodfile =
                    Utils.getPortletRequest().getParameter("UrlFiledowload");
        } catch (Exception e) {
            // TODO: Add catch code
            e.printStackTrace();
        }

        return dowlaodfile;
    }
    
    private String Tieudevanban() {
        String tieudeCtvanban = "Chi tiết văn bản";
        try {
            tieudeCtvanban = Utils.getPortletRequest().getParameter("tieudechitietvb");
        } catch (Exception e) {
            // TODO: Add catch code
            e.printStackTrace();
        }

        return tieudeCtvanban;
    }
    
    private String VanbanId() {
        String Idvanban = "";
        try {
            Idvanban = Utils.getPortletRequest().getParameter("vanbanid");
        } catch (Exception e) {
            // TODO: Add catch code
            e.printStackTrace();
        }

        return Idvanban;
    }
    
    public void setVanbanId(String vanbanId) {
        this.vanbanId = vanbanId;
    }

    public String getVanbanId() {
        vanbanId = VanbanId();
        return vanbanId;
    }

    public void setChitietvanbanUrl(String chitietvanbanUrl) {
        this.chitietvanbanUrl = chitietvanbanUrl;
    }

    public String getChitietvanbanUrl() {
        chitietvanbanUrl = UrlChitietvanban();
        return chitietvanbanUrl;
    }

    public void setUrlFiledowload(String UrlFiledowload) {
        this.UrlFiledowload = UrlFiledowload;
    }

    public String getUrlFiledowload() {
        UrlFiledowload = GetUrlDowloadfile();
        return UrlFiledowload;
    }

    public void setTieudechitietvb(String tieudechitietvb) {
        this.tieudechitietvb = tieudechitietvb;
    }

    public String getTieudechitietvb() {
        tieudechitietvb = Tieudevanban();
        return tieudechitietvb;
    }

    public String getYKienLabel() {
        return Utils.getResource("Y_KIEN_THAM_GIA");
    }

    public String getDiaChiLabel() {
        return Utils.getResource("DIA_CHI_LABEL");
    }
    
    public String getPhoneLabel(){
        return Utils.getResource("PHONE_LABEL");        
    }

    public String getChitietLabel() {
        return Utils.getResource("CHI_TIET_VAN_BAN");
    }

    public String getVBKhacLabel() {
        return Utils.getResource("VAN_BAN_KHAC");
    }

    public String getFileLabel() {
        return Utils.getResource("FILE_DINH_KEM");
    }

    public String getLangid() {
        try {
            langid = Utils.getPortalLocale().getLanguage();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return langid;
    }
}
